package com.example.mybatisplus.controller;

import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.model.domain.EndReport;
import com.example.mybatisplus.service.EndReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-05-29
 * @version v1.0
 */
@Controller
@RequestMapping("/api/endReport")
public class EndReportController {

    private final Logger logger = LoggerFactory.getLogger( EndReportController.class );

    @Autowired
    private EndReportService endReportService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        EndReport  endReport =  endReportService.getById(id);
        return JsonResponse.success(endReport);
    }

    /**
    * 描述：根据Id删除
    *
    */
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
//    @ResponseBody
//    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
//        endReportService.removeById(id);
//        return JsonResponse.success(null);
//    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateEndReport(EndReport  endReport) throws Exception {

            endReportService.updateById(endReport);
            return JsonResponse.success(null);
    }



    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(EndReport  endReport) throws Exception {
        endReportService.save(endReport);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建EndReport
     *
     */
    @RequestMapping(value = "/addEndReport", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addEndReport(Long stuId) throws Exception{
        boolean isExist = endReportService.isExist(stuId);
        if (isExist){
            return JsonResponse.failure("已存在该结案报告，别点了");
        }else {
            endReportService.addEndReport(stuId);
            return JsonResponse.success("增加学生结案报告成功");
        }
    }

    /**
     * 描述:获取全部结案报告数据
     *
     */
    @RequestMapping(value = "/getallEndReport", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getallEndReport()throws Exception {
        List<EndReport> endReports =  endReportService.getAllEndReport();

        return JsonResponse.success(endReports);
    }
}

